<?php

namespace App\Http\Controllers\Api;

use App\Models\Book;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Post as PostCollection;

class PostsController extends Controller
{

    /**
     *
     * @api {get} /posts 获取帖子
     * @apiName Get posts
     * @apiGroup Posts
     *
     * @apiParam {int} [bar_id] 吧id
     * @apiParam {int} [book_id] 书籍id
     *
     * @apiVersion 1.0.0
     * @apiSampleRequest /api/posts
     *
     */
    public function index(Request $request){

        $this->validate($request,[

            'bar_id' => 'required_without:book_id|exists:bar,id',
            'book_id' => 'required_without:bar_id|exists:books,id'

        ]);

        if ($request->has('bar_id')){
            // todo
            return 'bar posts';
        }

        $book = Book::find($request->book_id);

        $comments = $book->comments()->paginate(20);
        return PostCollection::collection($comments);

    }
}
